function getUserApi() {
  return new Promise((resolve) => {
    const timer = setTimeout(() => {
      clearTimeout(timer)
      resolve({ name: 'xxxx', age: 18 })
    }, 2000)
  })
}

/*
  模型的定义
 */
class User {
  _resolves: any[]
  constructor() {
    this._resolves = []
  }
  get data() {
    return new Promise((resolve) => {
      const user = sessionStorage.getItem('user')
      if (user) {
        return resolve(JSON.parse(user))
      } else {
        this._resolves.push(resolve)
      }
    })
  }
  set data(data: any) {
    this._resolves.forEach((resolve) => resolve(data))
    sessionStorage.setItem('user', data)
  }
  async loadUser() {
    sessionStorage.removeItem('user')
    const data = await getUserApi()
    this.data = data
  }
}
// 使用单例
export const user = new User()
